// 页面路径：store/index.js
import { createStore } from 'vuex'
// const obj = import.meta.globEager("./modules/*.js");
const obj = import.meta.glob('./modules/*.js', { eager: true })// 查找文件
import { getSystemTime, inventoryStaff } from '@/api/api.js'
const modules = Object.keys(obj).reduce((modules, modulePath) => {
	const moduleName = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, '$1');
	modules[moduleName] = obj[modulePath].default;
	return modules;
}, {});
const store = createStore({
	modules,
	state: {//存放状态
		serveSetingBool: 0,//服务设置监听对象
		distributedOutData: [],
		k3distributedOutData: [],
		iqcInfo: {},
		//系统状态栏高度
		statusBarHeight: 0,
		// 头部高度
		navHeight: 0,
		//成品检测扫描条码数据
		fqcScanList: [],
		// 列表页带数据进详情页
		fqcInfo: {},
		delStockManageList: [], //删除扫码列表
		wmsScanList: [], // wms扫码数组
		transferredList: [], // 调入数组
		allotList: [], // 调拨数组
		warehouseChangeList: [],// 库内调整 
		simpleMaterialList: [], //简单生产领料数组
		finishedProductStorageList: [],//成品入库
		systemTime: '',//当前服务器时间
		stockRemovalList: [],// 销售出库
		stockRemovalReturnList: [],// 销售出库退货
		checkList: [],//盘点
		fromData: '',//当前签名的文件流
		fromDataInventory: '',//成品盘点当前签名的文件流
		simpleWarehousingList: [],//简单生产入库数组
		transferRawMaterialList: [],//原材料转移扫描条码数据
		forLabelingMaterialList: [],//退料点击确定存储的记录
		palletTrayList: [], // 托盘合并的托盘扫描记录
		iotScanning: [], //IOT扫描数组
		finishedProductAdditional: [],//成品补录
		finishedProductPanDian: [], //成品盘点
		barcodeChangeList: [], //条码变更
		productReturnList: [],  //成品退货退料
		allotListSplit: [], // 原材料调拨（部分调拨）数组
		slottedAshOutList: [], // 开槽灰出库 数组
		scrapMarkList: [],//废品标记
		ngDetail: [],//NG明细
		semiFinishedCrushed: [], // 半成品次品粉碎
	},
	actions: {
		/**
		 * 获取当前服务器时间
		 * @param {*} param0  
		 * @returns 
		 */
		getSystemTimeRequest({ commit }) {
			return new Promise((resolve, reject) => {
				getSystemTime({}).then(res => {
					if (res.status == 200) {
						resolve(res.data.interceptDate)
					} else {
						resolve('')
					}
				}).catch(error => {
					reject(error)
				})
			})
		},
		/**
		 * 校验当前用户是否有盘点资格
		 * @param {*} param0  
		 * @returns 
		 */
		checkUser({ commit }) {
			return new Promise((resolve, reject) => {
				inventoryStaff({}).then(res => {
					if (res.status == 200) {
						resolve(res.data)
					} else {
						resolve(false)
					}
				}).catch(error => {
					reject(error)
				})
			})
		}
	},
	mutations: {
		addDistributedOutData(state, item) {
			console.log(item, 'mutationsmutationsmutations')
			state.distributedOutData.unshift(item);
			console.log(state.distributedOutData, 'state.distributedOutDatastate.distributedOutData')
		},
		deleteDistributedOutData(state, index) {
			state.distributedOutData.splice(index, 1);
		},
		removeDistributedOutData(state) {
			state.distributedOutData = []
		},
		
		
		addk3DistributedOutData(state, item) {
			console.log(item, 'mutationsmutationsmutations')
			state.k3distributedOutData.unshift(item);
			console.log(state.k3distributedOutData, 'state.k3distributedOutData.distributedOutData')
		},
		deleteK3DistributedOutData(state, index) {
			state.k3distributedOutData.splice(index, 1);
		},
		removeK3DistributedOutData(state) {
			state.k3distributedOutData = []
		},
		
		
		
		
		editIqcInfo(state, item) {
			state.iqcInfo = item
		},
		editIStatusBarHeight(state, item) {
			state.statusBarHeight = item
			// #ifdef APP-PLUS
			state.navHeight = Number(item) + 30 + 1;
			// #endif
			// #ifndef APP-PLUS
			state.navHeight = Number(item) + 44 + 1;
			// #endif
			console.log(state.navHeight, 'vuex');
		},
		addFqcScanList(state, item) {
			state.fqcScanList.unshift(item);
		},
		removeFqcScanList(state) {
			state.fqcScanList = [];
		},
		deleteFqcScanList(state, item) {
			console.log(item, 'item')
			console.log(typeof item == 'number', 'item465465')
			// 如果item是数字类型 单个删除
			if (typeof item == 'number') {
				state.fqcScanList.splice(Number(item), 1)
			} else {
				//如果是非数字类型 过滤掉两个数组焦急  批量删除
				let itemIds = [];
				item.forEach(x => {
					itemIds.push(x.barcode)
				})
				let list = state.fqcScanList.filter(items => {
					if (!itemIds.includes(items.barcode)) return items;
				})
				state.fqcScanList = list
			}
			console.log(state.fqcScanList, 'vuex');
		},
		editFqcInfo(state, item) {
			state.fqcInfo = item
		},
		addTransferredList(state, item) {
			state.transferredList.unshift(item);
		},
		deleteTransferredList(state, index) {
			state.transferredList.splice(index, 1);
		},
		clearTransferredList(state) {
			state.transferredList = []
			console.log(state.transferredList, 'state.transferredList')
		},
		/**
		 * 物料调拨 操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addAllotList(state, item) {
			state.allotList.unshift(item);
		},
		deleteAllotList(state, index) {
			state.allotList.splice(index, 1);
		},
		reactAllotList(state, index) {
			state.allotList = [];
		},
		/**
		 * 物料调拨（部分调拨） 操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addAllotListSplit(state, item) {
			state.allotListSplit.unshift(item);
		},
		deleteAllotListSplit(state, index) {
			state.allotListSplit.splice(index, 1);
		},
		reactAllotListSplit(state, index) {
			state.allotListSplit = [];
		},
		/**
		 * 库内调准 操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addWarehouseChangeList(state, item) {
			state.warehouseChangeList.unshift(item);
		},
		deleteWarehouseChangeList(state, index) {
			state.warehouseChangeList.splice(index, 1);
		},
		reactWarehouseChangeList(state, index) {
			state.warehouseChangeList = [];
		},
		/**
		 * 简单生产领料
		 * @param {*} state 
		 * @param {*} item  
		 */
		addSimpleMaterialList(state, item) {
			state.simpleMaterialList.unshift(item);
		},
		clearSimpleMaterialList(state) {
			state.simpleMaterialList = [];
		},
		deleteSimpleMaterialList(state, index) {
			state.simpleMaterialList.splice(index, 1);
		},

		/**
		 * 成品入库操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addFinishedProductStorage(state, item) {
			state.finishedProductStorageList.unshift(item);
		},
		deleteFinishedProductStorage(state, index) {
			state.finishedProductStorageList.splice(index, 1);
		},
		reactFinishedProductStorage(state) {
			state.finishedProductStorageList = [];
		},

		//barcode 删除store 数据
		deleteFinishedProductStorageByBarcode(state, barcode) {
			state.finishedProductStorageList = state.finishedProductStorageList.filter(e => e.barcode != barcode);

		},
		/**stockRemoval
		 * @param {*} state 
		 * @param {*} item 
		 */
		addStockRemovalList(state, item) {
			state.stockRemovalList.unshift(item);
		},
		deleteStockRemovalList(state, index) {
			state.stockRemovalList.splice(index, 1);
		},
		reactStockRemovalList(state) {
			state.stockRemovalList = [];
		},
		/**销售出库退货
		 * @param {*} state 
		 * @param {*} item 
		 */
		addStockRemovalReturnList(state, item) {
			state.stockRemovalReturnList.unshift(item);
		},
		deleteStockRemovalReturnList(state, index) {
			state.stockRemovalReturnList.splice(index, 1);
		},
		reactStockRemovalReturnList(state) {
			state.stockRemovalReturnList = [];
		},
		/**
		 * 存储当前图片流
		 * @param {*} state 
		 * @param {*} item   
		 */
		addFormData(state, params) {
			state.fromData = params
		},
		/**
		 * 存储当前图片流 成品盘点
		 * @param {*} state 
		 * @param {*} item   
		 */
		addFormDataInventory(state, params) {
			state.fromDataInventory = params
		},
		/**
		 * 删除当前图片流
		 * @param {*} state 
		 */
		delFormData(state) {
			state.fromData = ''
		},
		/**
		* 删除当前图片流 成品盘点
		* @param {*} state 
		*/
		delFormDataInventory(state) {
			state.fromDataInventory = ''
		},
		/**
		 * 更新当前服务设置状态 serveSetingBool
		 */
		updateServeSetingBool(state, params) {
			console.log(params, '----params-----')
			state.serveSetingBool = params
			console.log(state.serveSetingBool, '----params-----')
		},
		/**
		 * 简单生产入库 
		 */
		addsimpleWarehousingList(state, item) {
			state.simpleWarehousingList.push(item);
		},
		clearsimpleWarehousingList(state) {
			state.simpleWarehousingList = [];
		},
		deletesimpleWarehousingList(state, index) {
			state.simpleWarehousingList.splice(index, 1);
		},

		/**
		 * 原材料转移
		 * @param {*} state 
		 * @param {*} item  
		 */
		addTransferRawMaterialList(state, item) {
			state.transferRawMaterialList.push(item);
		},
		clearTransferRawMaterialList(state) {
			state.transferRawMaterialList = [];
		},
		deleteTransferRawMaterialList(state, index) {
			state.transferRawMaterialList.splice(index, 1);
		},
		/**
		 * 生产退料 
		 */
		addforLabelingMaterialList(state, item) {
			state.forLabelingMaterialList.unshift(item);
			console.log(state.forLabelingMaterialList, '----state.forLabelingMaterialList----')
		},
		clearforLabelingMaterialList(state) {
			state.forLabelingMaterialList = [];
		},
		deleteforLabelingMaterialList(state, index) {
			state.forLabelingMaterialList.splice(index, 1);
		},
		/**
		 * 托盘合并
		 * @param {*} state 
		 * @param {*} item  
		 */
		addPalletTrayList(state, item) {
			state.palletTrayList.unshift(item);
		},
		clearPalletTrayList(state) {
			state.palletTrayList = [];
		},
		deletePalletTrayList(state, index) {
			state.palletTrayList.splice(index, 1);
		},
		/**
		 * IOT扫描
		 * @param {*} state 
		 * @param {*} item  
		 */
		addIotScanningList(state, item) {
			state.iotScanning.push(item);
		},
		clearIotScanningList(state) {
			state.iotScanning = [];
		},
		deleteIotScanningList(state, index) {
			state.iotScanning.splice(index, 1);
		},
		/**
		 * 成品补录操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addFinishedProductAdditional(state, item) {
			state.finishedProductAdditional.unshift(item);
		},
		deleteFinishedProductAdditionalStorage(state, index) {
			state.finishedProductAdditional.splice(index, 1);
		},
		clearFinishedProductStorage(state) {
			state.finishedProductAdditional = [];
		},
		//barcode 删除store 数据
		deleteFinishedProductAdditionalStorageByBarcode(state, barcode) {
			state.finishedProductAdditional = state.finishedProductAdditional.filter(e => e.barcode != barcode);

		},

		/**
		 * 成品盘点
		 **/

		//添加
		addfinishedProductPanDian(state, item) {
			state.finishedProductPanDian.unshift(item);
		},
		//删除
		deletefinishedProductPanDian(state, index) {
			state.finishedProductPanDian.splice(index, 1);
		},
		//清空
		clearfinishedProductPanDian(state) {
			state.finishedProductPanDian = [];
		},

		/**
		* 条码变更
		**/
		//添加
		addBarcodeChangeList(state, item) {
			state.barcodeChangeList.unshift(item);
		},
		//删除
		deleteBarcodeChangeList(state, index) {
			state.barcodeChangeList.splice(index, 1);
		},
		//清空
		clearBarcodeChangeList(state) {
			state.barcodeChangeList = [];
		},
		//barcode 删除store 数据
		deleteBarcodeChangeListStorage(state, barcode) {
			state.barcodeChangeList = state.barcodeChangeList.filter(e => e.barcode != barcode);

		},
		/**
	 * 半成品次品粉碎
	 * @param {*} state 
	 * @param {*} item 
	 **/
		//添加
		addSemiFinishedCrushed(state, item) {
			state.semiFinishedCrushed.unshift(item);
		},
		//删除
		deleteSemiFinishedCrushed(state, index) {
			state.semiFinishedCrushed.splice(index, 1);
		},
		//清空
		clearSemiFinishedCrushed(state) {
			state.semiFinishedCrushed = [];
		},
		/**
		 * 成品退货退料
		 **/
		//添加
		addProductReturnList(state, item) {
			state.productReturnList.unshift(item);
		},
		//删除
		deleteProductReturnList(state, index) {
			state.productReturnList.splice(index, 1);
		},
		//清空
		clearProductReturnList(state) {
			state.productReturnList = [];
		},
		/**
		 * 开槽灰出库
		 * @param {*} state 
		 * @param {*} item  
		 */
		addSlottedAshOutList(state, item) {
			state.slottedAshOutList.unshift(item);
		},
		clearSlottedAshOutList(state) {
			state.slottedAshOutList = [];
		},
		deleteSlottedAshOutList(state, index) {
			state.slottedAshOutList.splice(index, 1);
		},

		/**
		 * 废品标记操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addScrapMark(state, item) {
			state.scrapMarkList.unshift(item);
		},
		deleteScrapMark(state, index) {
			state.scrapMarkList.splice(index, 1);
		},
		clearScrapMark(state) {
			state.scrapMarkList = [];
		},
		/**
		 * 废品标记操作
		 * @param {*} state 
		 * @param {*} item 
		 */
		addNGDetail(state, item) {
			state.ngDetail.unshift(item);
		},
		deleteNGDetail(state, index) {
			state.ngDetail.splice(index, 1);
		},
		clearNGDetail(state) {
			state.ngDetail = [];
		}

	}
})

export default store